//
//  NotificationService.swift
//  newPush
//
//  Created by zz on 20.3.24.
//

import UIKit
import UserNotifications



class NotificationService: UNNotificationServiceExtension {
    
    var contentHandler: ((UNNotificationContent) -> Void)?
    var bestAttemptContent: UNMutableNotificationContent?
    
    var isSound:Bool = false
    
    override func didReceive(_ request: UNNotificationRequest, withContentHandler contentHandler: @escaping (UNNotificationContent) -> Void) {
        
        /**
         {"aps":{"alert":{"title":"title","body":"body"},"mutable-content":1}}
         
         */
        
        
        self.contentHandler = contentHandler
        bestAttemptContent = (request.content.mutableCopy() as? UNMutableNotificationContent)
        
        let amount = bestAttemptContent?.body  ?? ""
        bestAttemptContent?.body = amount + "到账了"
        let monery = OPBApnsHelper.shared.getMusicArray(with: amount)
        //        let name = OPBApnsHelper.shared.mergeVoice(musicArry: monery)
        OPBApnsHelper.shared.mergeVoiceAudiFileName(musicArry: monery) {[weak self] audioFileName in
            if let name = audioFileName {
                let sound = UNNotificationSound(named: UNNotificationSoundName(name))
                self?.bestAttemptContent?.sound = sound
                if let bestAttemptContent = self?.bestAttemptContent {
                    contentHandler(bestAttemptContent)
                }
            }
        }
        
//        speakContent("就是啊")
//        if let bestAttemptContent = self.bestAttemptContent {
//            contentHandler(bestAttemptContent)
//        }
    }
    
    

    
    override func serviceExtensionTimeWillExpire() {
        // Called just before the extension will be terminated by the system.
        // Use this as an opportunity to deliver your "best attempt" at modified content, otherwise the original push payload will be used.
        if let contentHandler = contentHandler, let bestAttemptContent =  bestAttemptContent {
            contentHandler(bestAttemptContent)
        }
    }
    
}
